package com.meituan.catering.management.order.biz.model.converter;

import com.google.common.collect.Lists;
import com.meituan.catering.management.order.api.http.model.request.AdjustCateringOrderHttpRequest;
import com.meituan.catering.management.order.biz.model.request.AdjustCateringOrderBizRequest;

import java.util.List;
import java.util.Objects;

/**
 * @author Believe
 * Date 2022/6/6 9:32
 */
public class AdjustCateringOrderBizRequestConverter {
    public static AdjustCateringOrderBizRequest toAdjustCateringOrderBizRequest(AdjustCateringOrderHttpRequest request) {
        if (Objects.isNull(request)) {
            return null;
        }
        AdjustCateringOrderBizRequest adjustCateringOrderBizRequest = new AdjustCateringOrderBizRequest();
        adjustCateringOrderBizRequest.setVersion(request.getVersion());
        adjustCateringOrderBizRequest.setItems(toItem(request.getItems()));
        return adjustCateringOrderBizRequest;
    }

    private static List<AdjustCateringOrderBizRequest.Item> toItem(List<AdjustCateringOrderHttpRequest.Item> itemList) {
        List<AdjustCateringOrderBizRequest.Item> items = Lists.newLinkedList();
        for (AdjustCateringOrderHttpRequest.Item item : itemList) {
            AdjustCateringOrderBizRequest.Item bizItem = new AdjustCateringOrderBizRequest.Item();
            bizItem.setSeqNo(item.getSeqNo());
            bizItem.setVersion(item.getVersion());
            bizItem.setQuantityOnAdjustment(item.getQuantityOnAdjustment());
            bizItem.setProductId(item.getProductId());
            bizItem.setProductMethodId(item.getProductMethodId());
            bizItem.setAccessories(toAccessory(item.getAccessories()));
            items.add(bizItem);
        }
        return items;
    }

    private static List<AdjustCateringOrderBizRequest.Item.Accessory> toAccessory(List<AdjustCateringOrderHttpRequest.Item.Accessory> accessoryList) {
        List<AdjustCateringOrderBizRequest.Item.Accessory> accessories = Lists.newLinkedList();
        for (AdjustCateringOrderHttpRequest.Item.Accessory accessory : accessoryList) {
            AdjustCateringOrderBizRequest.Item.Accessory bizAcc = new AdjustCateringOrderBizRequest.Item.Accessory();
            bizAcc.setSeqNo(accessory.getSeqNo());
            bizAcc.setVersion(accessory.getVersion());
            bizAcc.setQuantityOnAdjustment(accessory.getQuantityOnAdjustment());
            bizAcc.setProductAccessoryId(accessory.getProductAccessoryId());
            accessories.add(bizAcc);
        }
        return accessories;
    }
}
